import app from '@/app'
import { get, update, code } from '@/api/erp/config'
import math from '@/app/math'

import base from '@/app/model'

// 继承总模型
const model = new base;

model.get = () => {
  return new Promise((resolve, jeject) => {
    get().then(res => {
      model.setData(res)
      resolve(res)
    }).catch(res => {
      jeject(res)
    })
  })
}

model.update = () => {
  return new Promise((resolve, jeject) => {
    update(model.getData()).then(res => {
      model.get()
      app.ui.message('success', res.message)
      resolve(res)
    }).catch(res => {
      app.ui.message('error', 'error.' + res.message)
      jeject(res)
    })
  })
}

// 获取code
model.code = name => {
  return new Promise((resolve, jeject) => {
    code(name).then(res => {
      resolve(res.code)
    }).catch(res => {
      jeject(res)
    })
  })
}

// 获取默认折扣
model.orderDiscount = () => {
  return model.getData().orders.discount
}

// 获取快捷备注
model.orderRemarks = () => {
  return model.getData().orders.remarks
}

// 获取收银台商品状态
model.orderGoodsType = () => {
  return model.getData().orders.goods_type
}

// 获取收银台默认类型
model.orderCodeType = () => {
  return model.getData().orders.code_type ?? 'print'
}

// 获取收银台自动打印
model.orderAutoPrint = () => {
  return model.getData().orders.auto_print ?? 0
}

// 获取收银台隔离
model.orderCheckoutEvery= () => {
  return model.getData().orders.checkout_every ?? 0
}

// 税
model.taxType = () => {
  return model.getData().tax_type ?? 0
}

// 获取客户积分比例
model.clientPoint = money => {
  return math.mul(money, model.getData().client_point)
}

// 小票限制金额
model.printNorm = money => {
  return Number(model.getData().print_norm) > Number(money)
}

// 代金劵支付方式
model.voucherPay = () => {
  return {
    pay_id: 0,
    pay_name: model.getData().voucher.pay
  }
}

// 获取二级密码
model.levelPassword = () => {
  return model.getData().level_password
}

export default model